package com.luyue.zagj.activity.home;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.luyue.zagj.MyApplication;
import com.luyue.zagj.R;
import com.luyue.zagj.application.Constant;


import com.luyue.zagj.utils.DrawableUtil;
import com.luyue.zagj.utils.ToastUtil;
import com.tianditu.android.maps.GeoPoint;
import com.tianditu.android.maps.MapController;
import com.tianditu.android.maps.MapView;
import com.tianditu.android.maps.MapViewRender;
import com.tianditu.android.maps.MyLocationOverlay;
import com.tianditu.android.maps.Overlay;
import com.tianditu.android.maps.TErrorCode;
import com.tianditu.android.maps.TGeoAddress;
import com.tianditu.android.maps.TGeoDecode;
import com.tianditu.android.maps.overlay.MarkerOverlay;
import com.tianditu.android.maps.renderoption.DrawableOption;

import javax.microedition.khronos.opengles.GL10;

public class ShowLocationActivity extends Activity implements TGeoDecode.OnGeoResultListener ,View.OnClickListener{
    private MapView mMapView = null;

    public EditText mEditTextLat;
    public EditText mEditTextLon;
    private MarkerOverlay mOverlay;
    private String longitude;//经度
    private String latitude;//纬度
    private Button show_location_button;
    private EditText etShowlocation;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.location_activity);
        getLocationData();
        initLocation();
        initOverlay();

    }

    public void getLocationData() {
        show_location_button = (Button) findViewById(R.id.show_location_button);
        mEditTextLon = (EditText) findViewById(R.id.editTextLon);
        mEditTextLat = (EditText) findViewById(R.id.editTextLat);
        etShowlocation = (EditText) findViewById(R.id.et_showlocation_click);
        mMapView = (MapView) findViewById(R.id.mapview);
        show_location_button.setOnClickListener(this);
        //2.1获取保存的经纬度坐标
        SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
        longitude = sp.getString("longitude", "");
        latitude = sp.getString("latitude", "");
        mEditTextLon.setText(latitude);
        mEditTextLat.setText(longitude);
    }
    public void initLocation() {
        //设置启用内置的缩放控件
        mMapView.setBuiltInZoomControls(true);
        //得到mMapView的控制权,可以用它控制和驱动平移和缩放
        MapController mMapController = mMapView.getController();
        SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
        longitude = sp.getString("longitude", "");
        latitude = sp.getString("latitude", "");
        GeoPoint geoPoint = new GeoPoint((int) ( Double.valueOf(latitude) * 1E6), (int) ( Double.valueOf(longitude) * 1E6));
        searchGeoDecode(geoPoint);
      /*  //设置地图中心点
        mMapController.setCenter(geoPoint);
        //设置地图zoom级别
        mMapController.setZoom(13);*/
    }

    public MyOverlay clickOverlay;
    private void initOverlay() {
       /* MyLocationOverlay myLocation = new MyLocationOverlay(this, mMapView);
        myLocation.enableCompass();  //显示指南针
        myLocation.enableMyLocation(); //显示我的位置
        mMapView.getOverlays().add(myLocation);
*/
      /* Drawable drawable = getResources().getDrawable(R.mipmap.apply_brand);
        DrawableOption option = new DrawableOption();
        option.setAnchor(0.5f, (54.0f - 9.0f) / 54.0f);
        mOverlay = new MarkerOverlay();
        mOverlay.setOption(option);
        mOverlay.setPosition(mGeoPoint);
        mOverlay.setIcon(drawable);
        mMapView.addOverlay(mOverlay);
        mMapView.getController().setMapBound(mGeoPoint, 11);
*/
        MyLocationOverlay mLocation = new MyLocationOverlay(this, mMapView);
        mLocation.enableMyLocation();
        mMapView.getController().setCenter(mLocation.getMyLocation());
        mMapView.addOverlay(mLocation);

        mOverlay = new MarkerOverlay();
        mMapView.addOverlay(mOverlay);


        clickOverlay = new MyOverlay(new ShowLocationActivity());
        mMapView.addOverlay(clickOverlay);


    }

    private GeoPoint mGeoPoint;
    private TGeoDecode mGeoDecode;

    /**
     * 搜索得到的坐标实际位置
     * @param geoPoint
     * 得到的坐标实际位置
     * @param geoPoint 位置
     */
    private void searchGeoDecode(GeoPoint geoPoint) {
        mGeoPoint = geoPoint;

       // mMapView.getController().setMapBound(mGeoPoint, 11);

        // 开始搜索
        if (mGeoDecode == null)
            mGeoDecode = new TGeoDecode(this);
        mGeoDecode.search(geoPoint);
    }
    public int clickLocation=1;

    /**
     * TGeoDecode回调搜索的实现接口
     * @param addr 解析地址
     * @param errCode 错误码
     */
    @Override
    public void onGeoDecodeResult(TGeoAddress addr, int errCode) {
        if (errCode != TErrorCode.OK) {
            Toast.makeText(this,"获取地址失败 point =  " + mGeoPoint.toString() + ", error = " + errCode, Toast.LENGTH_SHORT).show();
            return;
        }
        if (addr == null) {
            Toast.makeText(this,"获取地址失败 point =  " + mGeoPoint.toString(),Toast.LENGTH_SHORT).show();
            return;
        }
        if (clickLocation==1) {
            mOverlay.setPosition(mGeoPoint);
            mMapView.getController().setZoom(15);
            //mMapView.getController().setMapBound(mGeoPoint, 11);
            String str = "当前位置:" + addr.getFullName() + "\n";
            Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
            etShowlocation.setVisibility(View.VISIBLE);
            etShowlocation.setText(addr.getFullName().substring(6));
            clickLocation=clickLocation+1;
        }else{
            String str = "";
            //str += "最近的poi名称:" + addr.getPoiName() + "\n";
            //str += "最近poi的方位:" + addr.getPoiDirection() + "\n";
            //str += "最近poi的距离:" + addr.getPoiDistance() + "\n";
            //str += "城市名称:" + addr.getCity() + "\n";
            str += "全称:" + addr.getFullName().substring(6)+ "\n";
            str += "最近的地址:" + addr.getAddress() + "\n";
            str += "最近地址的方位:" + addr.getAddrDirection() + "\n";
            //str += "最近地址的距离:" + addr.getAddrDistance() + "\n";
            str += "最近的道路名称:" + addr.getRoadName() + "\n";
           // str += "最近道路的距离:" + addr.getRoadDistance();
            Log.d("dasd",str);
            ToastUtil.getToast(str,ShowLocationActivity.this);
            etShowlocation.setVisibility(View.VISIBLE);
            etShowlocation.setText(addr.getFullName().substring(6));
        }
    }


    /**
     * 覆盖物处理点击事件
     * 覆盖物点击事件
     */

    public class MyOverlay extends Overlay {
        private Drawable d;
        private GeoPoint mGeoPoint;
        private DrawableOption mOption;
        public ShowLocationActivity activity;
        private final Paint mPaint;

        public MyOverlay(ShowLocationActivity activity) {
            this.activity = activity;
            d = MyApplication.getContext().getResources().getDrawable(R.mipmap.ac_repair_record);
            mOption = new DrawableOption();
            mOption.setAnchor(0.5f, 1.0f);
            mPaint = new Paint();
        }

        public void setGeoPoint(GeoPoint point) {
            mGeoPoint = point;
        }



        @Override
        public boolean onTap(GeoPoint point, MapView mapView) {
            mGeoPoint = point;
            mEditTextLon.setText(point.getLongitudeE6()+"");
            mEditTextLat.setText(point.getLatitudeE6()+"");
            searchGeoDecode(point);
            return true;
        }


        @Override
        public boolean onKeyUp(int keyCode, KeyEvent event, MapView mapView) {

            return super.onKeyUp(keyCode, event, mapView);
        }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event, MapView mapView) {

            return super.onKeyDown(keyCode, event, mapView);
        }


        @Override
        public boolean onLongPress(GeoPoint p, MapView mapView) {
            //mTvTips.setText("onLongPress:" + p.getLatitudeE6() + "," + p.getLongitudeE6());
            return super.onLongPress(p, mapView);
        }

        @Override
        public boolean isVisible() {
            return super.isVisible();
        }

        @Override
        public void setVisible(boolean b) {
            super.setVisible(b);
        }

        @Override
        public void draw(GL10 gl, MapView mapView, boolean shadow) {
            if (shadow)
                return;

            MapViewRender render = mapView.getMapViewRender();
            render.drawDrawable(gl, mOption, d, mGeoPoint);
        }
    }

    @Override
    public void onClick(View v) {
        if (etShowlocation==null){
            ToastUtil.getToast("网络异常,给您带来不便",this);
            finish();
            return;
        }
        Intent intent=new Intent();
        intent.putExtra("location",etShowlocation.getText().toString().trim());
        setResult(1,intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}



